﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class UITXT : ViewBase
    {

        #region ==IsTXTBox===========
        public static readonly DependencyProperty IsTXTBoxProperty = DependencyProperty.Register("IsTXTBox",
            typeof(bool), typeof(UITXT),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsTXTBoxChanged));
        public bool IsTXTBox
        {
            get
            {
                return (bool)GetValue(IsTXTBoxProperty);
            }
            set
            {
                SetValue(IsTXTBoxProperty, value);
            }
        }
        private static void OnIsTXTBoxChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UITXT;
            if (src != null)
            {
                src.IsTXTBox = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==IsInfoOneLine===========
        public static readonly DependencyProperty IsInfoOneLineProperty = DependencyProperty.Register("IsInfoOneLine",
            typeof(bool), typeof(UITXT),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsInfoOneLineChanged));
        public bool IsInfoOneLine
        {
            get
            {
                return (bool)GetValue(IsInfoOneLineProperty);
            }
            set
            {
                SetValue(IsInfoOneLineProperty, value);
            }
        }
        private static void OnIsInfoOneLineChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UITXT;
            if (src != null)
            {
                src.IsInfoOneLine = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==IsEditable===========
        public static readonly DependencyProperty IsEditableProperty = DependencyProperty.Register("IsEditable",
            typeof(bool), typeof(UITXT),
            new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsEditableChanged));
        public bool IsEditable
        {
            get
            {
                return (bool)GetValue(IsEditableProperty);
            }
            set
            {
                SetValue(IsEditableProperty, value);
            }
        }
        private static void OnIsEditableChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UITXT;
            if (src != null)
            {
                src.IsEditable = (bool)e.NewValue;
            }
        }
        #endregion
        #region ==TART===========
        public static readonly DependencyProperty TARTProperty = DependencyProperty.Register("TART",
            typeof(string), typeof(UITXT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTARTChanged));
        public string TART
        {
            get
            {
                return (string)GetValue(TARTProperty);
            }
            set
            {
                SetValue(TARTProperty, value);
                if (MV == null) return;
                ((VTXTIND)MV).TART = value;
            }
        }
        private static void OnTARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UITXT;
            if (src != null)
            {
                src.TART = (string)e.NewValue;
            }
        }
        #endregion

        #region ==PIND===========
        public static readonly DependencyProperty PINDProperty = DependencyProperty.Register("PIND",
            typeof(string), typeof(UITXT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPINDChanged));
        public string PIND
        {
            get
            {
                return (string)GetValue(PINDProperty);
            }
            set
            {
                SetValue(PINDProperty, value);
                if (MV == null) return;
                ((VTXTIND)MV).PIND = value;
            }
        }
        private static void OnPINDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UITXT;
            if (src != null)
            {
                src.PIND = (string)e.NewValue;
            }
        }
        #endregion

        #region ==SIND===========
        public static readonly DependencyProperty SINDProperty = DependencyProperty.Register("SIND",
            typeof(string), typeof(UITXT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSINDChanged));
        public string SIND
        {
            get
            {
                return (string)GetValue(SINDProperty);
            }
            set
            {
                SetValue(SINDProperty, value);
                LOADBYSIND(value);
                if (MV == null) return;
                ((VTXTIND)MV).SIND = value;
            }
        }
        private static void OnSINDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UITXT;
            if (src != null)
            {
                src.SIND = (string)e.NewValue;

            }
        }
        #endregion
        public VTXTIND TMV
        {
            get
            {
                return (VTXTIND)MV;
            }
        }
        public UITXT()
        {
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                PIND = "";
            }
            else
            {
                PIND = dwMaster["TXTIPIND"].ToString();
            }
        }

        public void LOADBYSIND(string sind)
        {
            if (TBMain == null)
                return;
            TBMain.Clear();
            if (sind.Length < 10)
            {
                return;
            }
            TMV.LOADBYSIND(sind,TART,IsAuto);
            vwMaster.View.MoveCurrentToFirst();
        }
        public override void OnAutoChg(bool value)
        {
            if (!IsAuto)
                return;
            LOADBYSIND(SIND);
            base.OnAutoChg(value);
        }
        public override void OnEdit()
        {
            if (!IsEdit)
            {
                IsAuto = false;
                return;
            }               
            IsAuto = true;
            base.OnEdit();
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            foreach (DataRow row in TBMain.Rows)
            {
                ((VTXTIND)MV).SaveTXTIND(row, app.company);
            }
            base.OnSave();
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            foreach (DataRow row in TBMain.Rows)
            {
                ((VTXTIND)MV).SaveTXTIND(row, app.company);
            }
            base.SaveCommand(sender, e);
             
        }
    }
}
